/**
 * 
 */
package nl.boektrust.btsoep.shared.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.googlecode.objectify.annotation.Serialized;
import com.googlecode.objectify.annotation.Subclass;
import com.googlecode.objectify.annotation.Unindexed;

/**
 * @author rao3
 *
 */
@Subclass
public class BookManifestation extends Book implements Serializable,IsSerializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4991525343474923212L;
	
	
	 private long recordReference;
	 @Unindexed private ProductFormCode productFormCode;
	 @Unindexed private int numberOfPages;
	 @Unindexed private boolean illustrated;
	 @Unindexed private int numberOfColorIllustrations;
	 @Unindexed private int numberOfBWIllustrations;
	 @Unindexed private Date publicationDate;
	 @Unindexed private Date firstPublicationDate;
	 @Unindexed private int edition;
	 @Unindexed private String imprint;
	 @Unindexed private EditionTypeCode editionTypeCode;
	 @Unindexed private String seriesTitle;
	 @Unindexed private String setTitle;
	@Serialized @Unindexed private ISBN isbn;
	@Serialized @Unindexed private Publisher publisher;
	//@Serialized SupplyDetail aSupplyDetail;
	
	@Serialized @Unindexed private HashSet<SupplyDetail> supplyDetails;
	@Serialized @Unindexed transient private HashSet<ImageReference> imageReferences;
	private transient boolean selected;
	/**
	 * @return the selected
	 */
	public boolean isSelected() {
		return selected;
	}
	/**
	 * @param selected the selected to set
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	/**
	 * @return the imageReferences
	 */
	public HashSet<ImageReference> getImageReferences() {
		return imageReferences;
	}
	
	public ArrayList<ImageReference> getImageReferencesOnix() {
		return null;
	}
	/**
	 * @return the publisher
	 */
	public Publisher getPublisher() {
		return publisher;
	}
	

	/**
	 * @param publisher the publisher to set
	 */
	public void setPublisher(Publisher publisher) {
		this.publisher = publisher;
	}
	/**
	 * @return the supplyDetails
	 */
	public HashSet<SupplyDetail> getSupplyDetails() {
		return supplyDetails;
	}


	/**
	 * @param title
	 */
	public BookManifestation(String title) {
		super(title);
		this.edition = -1;
		this.editionTypeCode = EditionTypeCode.UNDEFINED;
		this.illustrated = false;
		this.imprint = new String("");
		this.numberOfBWIllustrations = -1;
		this.numberOfColorIllustrations = -1;
		this.numberOfPages = -1;
		this.productFormCode = ProductFormCode.UNDEFINED;
		this.selected = false;
		this.seriesTitle = new String("");
		this.setTitle = new String("");
		this.supplyDetails = new HashSet<SupplyDetail>();
		this.imageReferences = new HashSet<ImageReference>();
		this.isbn = new ISBN();
		this.setSelected(false);
		
	}
	/**
	 * 
	 */
	public BookManifestation() {
		this("");
	}
	/**
	 * @return the recordReference
	 */
	public long getRecordReference() {
		return recordReference;
	}
	
	/**
	 * @param recordReference the recordReference to set
	 */
	public void setRecordReference(long recordReference) {
		this.recordReference = recordReference;
	}
	/**
	 * @return the productFormCode
	 */
	public ProductFormCode getProductFormCode() {
		return productFormCode;
	}
	
	/**
	 * @param productFormCode the productFormCode to set
	 */
	public void setProductFormCode(ProductFormCode productFormCode) {
		this.productFormCode = productFormCode;
	}

	/**
	 * @return the numberOfPages
	 */
	public int getNumberOfPages() {
		return numberOfPages;
	}
	
	/**
	 * @param numberOfPages the numberOfPages to set
	 */
	public void setNumberOfPages(int numberOfPages) {
		this.numberOfPages = numberOfPages;
	}
	/**
	 * @return the illustrated
	 */
	public boolean isIllustrated() {
		return illustrated;
	}
	/**
	 * @param illustrated the illustrated to set
	 */
	public void setIllustrated(boolean illustrated) {
		this.illustrated = illustrated;
	}
	/**
	 * @return the numberOfColorIllustrations
	 */
	public int getNumberOfColorIllustrations() {
		return numberOfColorIllustrations;
	}
	
	/**
	 * @param numberOfColorIllustrations the numberOfColorIllustrations to set
	 */
	public void setNumberOfColorIllustrations(int numberOfColorIllustrations) {
		this.numberOfColorIllustrations = numberOfColorIllustrations;
	}
	/**
	 * @return the numberOfBWIllustrations
	 */
	public int getNumberOfBWIllustrations() {
		return numberOfBWIllustrations;
	}
	
	/**
	 * @param numberOfBWIllustrations the numberOfBWIllustrations to set
	 */
	public void setNumberOfBWIllustrations(int numberOfBWIllustrations) {
		this.numberOfBWIllustrations = numberOfBWIllustrations;
	}
	/**
	 * @return the publicationDate
	 */
	public Date getPublicationDate() {
		return publicationDate;
	}
	
	/**
	 * @param publicationDate the publicationDate to set
	 */
	public void setPublicationDate(Date publicationDate) {
		this.publicationDate = publicationDate;
	}
	/**
	 * @return the firstPublicationDate
	 */
	public Date getFirstPublicationDate() {
		return firstPublicationDate;
	}

	/**
	 * @param firstPublicationDate the firstPublicationDate to set
	 */
	public void setFirstPublicationDate(Date firstPublicationDate) {
		this.firstPublicationDate = firstPublicationDate;
	}
	/**
	 * @return the edition
	 */
	public int getEdition() {
		return edition;
	}
	
	/**
	 * @param edition the edition to set
	 */
	public void setEdition(int edition) {
		this.edition = edition;
	}
	/**
	 * @return the imprint
	 */
	public String getImprint() {
		return imprint;
	}
	
	/**
	 * @param imprint the imprint to set
	 */
	public void setImprint(String imprint) {
		this.imprint = imprint;
	}
	/**
	 * @return the editionTypeCode
	 */
	public EditionTypeCode getEditionTypeCode() {
		return editionTypeCode;
	}
	
	/**
	 * @param editionTypeCode the editionTypeCode to set
	 */
	public void setEditionTypeCode(EditionTypeCode editionTypeCode) {
		this.editionTypeCode = editionTypeCode;
	}
	/**
	 * @return the seriesTitle
	 */
	public String getSeriesTitle() {
		return seriesTitle;
	}
	
	/**
	 * @param seriesTitle the seriesTitle to set
	 */
	public void setSeriesTitle(String seriesTitle) {
		this.seriesTitle = seriesTitle;
	}
	/**
	 * @return the setTitle
	 */
	public String getSetTitle() {
		return setTitle;
	}
	
	/**
	 * @param setTitle the setTitle to set
	 */
	public void setSetTitle(String setTitle) {
		this.setTitle = setTitle;
	}
	
	
	/**
	 * @return the isbn
	 */
	public ISBN getIsbn() {
		return isbn;
	}
	
	/**
	 * @param isbn the isbn to set
	 */
	public void setIsbn(ISBN isbn) {
		this.isbn = isbn;
	}
	
	/** 
	 * @param isbn a string of the isbn to set
	 */
	public void setIsbn(String isbn) throws Exception {
		this.isbn = new ISBN(isbn);
	}

	
	public void addSupplyDetail(SupplyDetail s) {
		this.supplyDetails.add(s);
	}

	public String getPriceAsString(PriceTypeCode priceType) {
		String value = new String("");
		int price = getPrice(priceType);
		if (price == 0) {
			value = "Prijs onbekend";
		} else {
			if ((price%100) < 10) {
				value = "EUR. "+(price/100)+",0"+(price%100);
			} else {
				value = "EUR. "+(price/100)+","+(price%100);

			}
		}
		return value;
	}
	
	/* 
	 * returns 0 when price not found. 
	 */
	public int getPrice(PriceTypeCode priceType) {
		int fixedprice = 0;
		if (supplyDetails == null || supplyDetails.size() == 0) {
		} else {
			for (SupplyDetail s: supplyDetails) {
				if (s.getPriceArray() != null) {
					for (Price p : s.getPriceArray()) {
						if (p.getPriceTypeCode() == priceType) {
							fixedprice = p.getPriceAmountInCents();
						}
					}
				}
			}
		} 
		return fixedprice;
	}
	


	public int getMSRPPrice() {
		int price = 0;
		if (supplyDetails == null || supplyDetails.size() == 0) {
		} else {
			for (SupplyDetail s: supplyDetails) {
				if (s.getPriceArray() != null) {
					for (Price p : s.getPriceArray()) {
						if (p.getPriceTypeCode() == PriceTypeCode.MSRP) {
							price = p.getPriceAmountInCents();
						}
					}
				}
			}
		} 
		return price;
	}
	public String toPrintString() {
		String string = super.toPrintString();
		string = string + "recordreference:      " + id + "\n";
		string = string + "productformcode:      " + ProductFormCode.stringValue(productFormCode) + "\n";
		string = string + "number of pages:      " + numberOfPages + "\n";
		string = string + "illustrated:          " + illustrated + "\n";
		string = string + "number of color:      " + numberOfColorIllustrations + "\n";
		string = string + "number of BW:         " + numberOfBWIllustrations + "\n";
		if (publicationDate != null) string = string + "publicationDate:      " + publicationDate.toString() + "\n";
		if (firstPublicationDate != null) string = string + "firstPublicationDate: " + firstPublicationDate.toString() + "\n";
		string = string + "edition:              " + edition + "\n";
		if (imprint != null) string = string + "imprint:              " + imprint + "\n";
		string = string + "editionTypeCode:      " + EditionTypeCode.stringValue(editionTypeCode) + "\n";
		if (seriesTitle != null) string = string + "seriesTitle:          " + seriesTitle + "\n";
		if (setTitle != null) string = string + "setTitle:             " + setTitle + "\n";
		if (isbn != null) string = string + "isbn:                 " + isbn.getIsbn13() + "\n";
		if (publisher != null) string = string + "publisher:            " + publisher.getName() + "\n";
		for (SupplyDetail supplyDetail : supplyDetails) {
			string = string + "supplydetail:         " + "\n";
			string = string + "   availability:      " + AvailabilityCode.stringValue(supplyDetail.getAvailabilityCode()) + "\n";
			string = string + "	  modification date: " + supplyDetail.getModificationDate().toString() + "\n";
			string = string + "	  # prices         : " + supplyDetail.getPriceArray().size() + "\n";
		}

		return string;
	}
}
